package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SizeUtils
import com.lv.common.imp.OnPositionTListener
import com.lv.common.utils.showToast
import com.lv.common.widget.recyclerview.decoration.SpacesItemDecoration
import com.lv.service.R
import com.lv.service.adapter.ImageTextAdapter
import com.lv.service.adapter.ImageTextTagAdapter
import com.lv.service.bean.CommonSelectedBean
import com.lxj.xpopup.core.BottomPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述
 */
class CompanyOpenPayPopupView(
    private val mContext: Context,
    private val mTitle: String,
    private val mAllBeans: List<CommonSelectedBean>? = null,
    private val mTopH: Int = 0,
    private val mPositionTListener: OnPositionTListener<List<CommonSelectedBean>>? = null
) :
    BottomPopupView(mContext) {
    private var current = -1
    private var rvContent: RecyclerView? = null
    private var tvTitle: TextView? = null
    private var viewTop: View? = null
    private var viewShadow: TextView? = null
    private val mBeans = mutableListOf<CommonSelectedBean>()

    private var mAdapter: ImageTextTagAdapter? = null
    override fun getImplLayoutId(): Int {
        return R.layout.company_open_pay_popup
    }

    override fun onCreate() {
        super.onCreate()
        rvContent = findViewById<RecyclerView>(R.id.rv_content)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        viewTop = findViewById<View>(R.id.view_top)
        viewShadow = findViewById<TextView>(R.id.tv_submit)
        findViewById<ImageView>(R.id.iv_close).setOnClickListener { dismiss() }
        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = mTopH
        initView()
        initEvnet()
    }

    private fun initView() {
        mAllBeans?.let {
            mBeans.clear()
            mAllBeans.forEachIndexed { index, commonSelectedBean ->
                if (commonSelectedBean.selected == true) {
                    current = index
                }
            }
            val newArray = mAllBeans.map { it.copy() }.toTypedArray()
            mBeans.addAll(newArray)
        }
        tvTitle?.text = mTitle
//        val gridLayoutParams = GridLayoutManager(context, 5)
//        rvContent?.layoutManager = gridLayoutParams
//        rvContent?.addItemDecoration(
//            SpacesItemDecoration(
//                SizeUtils.dp2px(12.0f), SizeUtils.dp2px(12.0f), false
//            )
//        )
        mAdapter = ImageTextTagAdapter(mBeans)
        rvContent?.adapter = mAdapter

        mAdapter?.onViewItemClickListener = OnPositionTListener { position, data ->
            if (current == position) {
                return@OnPositionTListener
            }
            if (current >= 0) {
                mBeans[current].selected = false
                mAdapter?.notifyItemChanged(current, R.id.iv_selected)
            }
            current = position
            mBeans[current].selected = true
            mAdapter?.notifyItemChanged(current, R.id.iv_selected)
            if (mBeans[current].tag == "1") {
                mPositionTListener?.onClick(current, mBeans)
                dismiss()
            }
        }

    }

    private fun initEvnet() {

        viewTop?.setOnClickListener {

            dismiss()
        }

        viewShadow?.setOnClickListener {
            if (current == -1) {
                showToast("请选择")
                return@setOnClickListener
            }
            mPositionTListener?.onClick(current, mBeans)
            dismiss()
        }
    }

    private fun setDismissDialog() {
        dismiss()
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()


    }

}